/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * fastAddSpravka_Item.java
 *
 * Created on Jan 25, 2009, 11:37:56 PM
 */

package GUI.other;

import GUI.AddToSklad;
import functions.Distributors;
import functions.Dogy;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.sql.SQLException;

import javax.swing.JOptionPane;
import model.Distributor;
import model.Drug;
import model.Item;

/**
 *
 * @author JArik
 */
public class fastDistrAdd extends fastAdd {

    /** Creates new form fastAddSpravka_Item */
    private Distributor toRec;
    private AddToSklad parent;
    private String itemText, descrText;
    public fastDistrAdd(Distributor rec,AddToSklad nParent) {
        super(nParent);
        initComponents();
        parent = nParent;
        toRec = rec;

        itemText = "Название";
        descrText = "Описание предмета";
        if(!parent.getActiveFieldText().equals(""))
            distrName.setText(parent.getActiveFieldText());
        else
        {
          distrName.setText(itemText);
          distrName.setForeground(Color.gray);
        }
        descrField.setText(descrText);
        descrField.setForeground(Color.gray);

       
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        distrName = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        addButton = new javax.swing.JButton();
        descrField = new javax.swing.JTextField();

        setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true));

        jLabel1.setText("Быстрое добавление в каталог");

        distrName.setText("Name");
        distrName.setFocusCycleRoot(true);
        distrName.setNextFocusableComponent(descrField);
        distrName.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                distrNameFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                distrNameFocusLost(evt);
            }
        });
        distrName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                distrNameKeyReleased(evt);
            }
        });

        jButton1.setText("Закрыть");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        addButton.setText("Добавить");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        descrField.setText("Описание");
        descrField.setNextFocusableComponent(addButton);
        descrField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                descrFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                descrFieldFocusLost(evt);
            }
        });
        descrField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                descrFieldKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(distrName, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(descrField, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                        .addComponent(addButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(distrName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(descrField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        parent.disposeDialog();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
       try
       {
         if(distrName.getText().equals(itemText) || distrName.getText().equals(""))
             throw new Exception("Введите название поставщика.");
         Distributor distr = new Distributor();
         distr.setName(distrName.getText());
         if(!descrField.getText().equals(descrText) && !descrField.getText().equals(""))
             distr.setDescr(descrField.getText());
         else
             distr.setDescr("");
         int id=-1;
         //if( parent.getChoosenItem() instanceof Drug)
            id  = Distributors.addToCatalog(distr);
         //else if(parent.getChoosenItem() instanceof Item)
         //    id = Distributors.addOtherToCatalog(distr);
         distr.setId(id);
         toRec = distr;
         parent.setActiveFieldText(distr);
       } // try
       catch(Exception e)
       {
           JOptionPane.showMessageDialog(null, e.getMessage());
       }
       parent.disposeDialog();
}//GEN-LAST:event_addButtonActionPerformed

    private void distrNameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_distrNameFocusGained
       Dogy.focusGained(distrName, itemText);
}//GEN-LAST:event_distrNameFocusGained

    private void distrNameFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_distrNameFocusLost
        Dogy.focusLost(distrName, itemText);
}//GEN-LAST:event_distrNameFocusLost

    private void descrFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_descrFieldFocusGained
        Dogy.focusGained(descrField, descrText);
}//GEN-LAST:event_descrFieldFocusGained

    private void descrFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_descrFieldFocusLost
        Dogy.focusLost(descrField, descrText);
}//GEN-LAST:event_descrFieldFocusLost

    private void distrNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_distrNameKeyReleased
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
            addButtonActionPerformed(null);
    }//GEN-LAST:event_distrNameKeyReleased

    private void descrFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_descrFieldKeyReleased
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
            addButtonActionPerformed(null);
    }//GEN-LAST:event_descrFieldKeyReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JTextField descrField;
    private javax.swing.JTextField distrName;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables

}
